# _*_ coding utf-8 _*_
# george
# time: 2025/1/4上午9:34
# name: order.py
# comment:
from flask import Blueprint, render_template, session, redirect
from ..db import fetch_one, fetch_all

# 创建蓝图对象
od = Blueprint("order", __name__)

def update_status(data_list):
    # 状态定义
    status_dict = {
        0: "success",
        1: "fail",
        2: "waiting",
        3: "execute"
    }
    for item in data_list:
        print(item)
        item["status"] = status_dict[item["status"]]
    return data_list


# 显示订单
@od.route("/order/list")
def order_list():
    user_dict = session.get("user_info")
    print(user_dict)
    # {'id': 1, 'mobile': '11111111111', 'role': 0, 'username': 'zs'}

    if user_dict.get("role") == 1:  # 是用户
        sql = "select * from orders left outer join users on orders.user_id=users.id where orders.id=%s"
        data_list = fetch_all(sql, (user_dict["id"],))
        data_list = [user for user in data_list if user.get("active") != 1]
        update_status(data_list)

    else:  # 管理员
        sql = " select * from orders left outer join users on orders.user_id=users.id"
        data_list = fetch_all(sql, ())
        # 将active数据为1的数据隐藏
        data_list = [user for user in data_list if user.get("active") != 1]
        update_status(data_list)
        print(data_list)
    return render_template("order_list.html", data_list=data_list, user=user_dict["username"])


# 创建订单
@od.route("/order/create")
def create_list():
    return "创建订单"


# 删除订单
@od.route("/order/delete")
def delete_list():
    return "删除订单"
